package com.lc.afbx.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.ShopAdapter;
import com.lc.afbx.conn.GetApiInsuranceBaoJia;
import com.lc.afbx.conn.GetApiShowStore;
import com.lc.afbx.conn.GetApiVehileBaoJia;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.widget.MyListView;
import com.zcx.helper.http.AsyCallBack;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CompulsoryInsurancePriceActivity extends BaseActivity implements View.OnClickListener {

    private Button btnLiJiTouBao;
    private ShopAdapter adapter;
    private int selectPosition = -1;
    private List<GetApiShowStore.Data> dataList;

    private String baojia;
    private String cheChuanShui;
    private Bundle bundle;


    //请求  交强险报价
    private GetApiInsuranceBaoJia getApiInsuranceBaoJia = new GetApiInsuranceBaoJia(new AsyCallBack<GetApiInsuranceBaoJia.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiInsuranceBaoJia.Info info) throws Exception {

            baojia = info.data.premium;
            ((TextView) findViewById(R.id.premium_qiangxian)).setText(info.data.premium + "元");
            ((TextView) findViewById(R.id.relief_qiangxian)).setText(info.data.relief + "元");
            ((TextView) findViewById(R.id.companyName_qiangxian)).setText(info.data.title);
            ((SimpleDraweeView) findViewById(R.id.imgCompany)).setImageURI(Uri.parse(info.data.picurl));

        }
    });

    // 车船税报价 接口
    private GetApiVehileBaoJia getApiVehileBaoJia = new GetApiVehileBaoJia(new AsyCallBack<GetApiVehileBaoJia.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiVehileBaoJia.Info info) throws Exception {
            ((TextView) findViewById(R.id.nowfee)).setText(info.data.nowfee + "元");
            ((TextView) findViewById(R.id.backfee)).setText(info.data.backfee + "元");
            ((TextView) findViewById(R.id.fine)).setText(info.data.fine + "元");
            ((TextView) findViewById(R.id.total)).setText(bundle.getString("cheChuanShui") + "元");
//            cheChuanShui = info.data.total;
            cheChuanShui = bundle.getString("cheChuanShui");
        }
    });
    // 这是保险门店的接口
    private GetApiShowStore getApiShowStore = new GetApiShowStore(new AsyCallBack<GetApiShowStore.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowStore.Info info) throws Exception {

            dataList = info.dataList;
            mListView.setAdapter(adapter = new ShopAdapter(context, info.dataList));
        }
    });
    private MyListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_jiaoqiangxian_baojia);

        bundle = getIntent().getExtras();

        initView();

        setListener();

        getApiInsuranceBaoJia.cid = bundle.getString("cid");//公司id
        getApiInsuranceBaoJia.regTime = bundle.getString("regtime");//行驶证上注册日期
        getApiInsuranceBaoJia.carType = bundle.getString("cartype_str");//车辆类型
        getApiInsuranceBaoJia.useType = bundle.getString("usenature_str");//使用性质，营运非营运
        getApiInsuranceBaoJia.execute(this);

        getApiVehileBaoJia.cid = bundle.getString("cid");
        getApiVehileBaoJia.execute(this);

        getApiShowStore.cid = bundle.getString("cid");
        getApiShowStore.curcode = BaseApplication.BasePreferences.readCityID();
        getApiShowStore.execute(this);
    }

    private void setListener() {

        btnLiJiTouBao.setOnClickListener(this);

    }

    private void initView() {

        findViewById(R.id.back).setOnClickListener(this);

        btnLiJiTouBao = ((Button) findViewById(R.id.shouye_btn_jiaoqiangxian_baojia_lijitoubao));

        mListView = ((MyListView) findViewById(R.id.mListView));

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (adapter.isCheck.get(position) == false) {
                    adapter.init();
                    adapter.isCheck.put(position, true);
                    selectPosition = position;
                    adapter.notifyDataSetChanged();
                } else {
                    adapter.isCheck.put(position, false);
                    selectPosition = -1;
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public void onClick(View view) {

        Intent intent = null;
        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.shouye_btn_jiaoqiangxian_baojia_lijitoubao:
                intent = new Intent(this, CompulsoryInsuranceInsureActivity.class);


                if (selectPosition < 0) {
                    MyToast.showToast("请选择保投保门店", context);
                    return;
                }

//                UtilLog.e("===========================2", bundle.getString("insurance_type") + "sdfasdfsaasdfas");
                bundle.putString("shopName", dataList.get(selectPosition).title);
                bundle.putString("shopId", dataList.get(selectPosition).id);

                float aa = Float.parseFloat(baojia) + Float.parseFloat(cheChuanShui);
                bundle.putString("heji", new DecimalFormat(".00").format(aa));
                intent.putExtras(bundle);
                //TODO  这里该写  带过去的值
                break;

        }
        startActivity(intent);
    }


}
